package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.alarm.AlarmListHandleService;
import com.hllxd.hism.star.basic.service.service.alarm.AlarmListService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author wuguangxi
 * @description
 * @date 2023/11/14 17:51
 **/
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/alarm")
public class AlarmController2 {
    @Autowired
    private AlarmListService alarmListService;
    @Autowired
    private AlarmListHandleService alarmListHandleService;

    @Data
    public static class Param {
        private String alarmId;
        private String id;
    }

    @RestGrpcMapping("/findByAlarmId")
    public NTSTATUS findByAlarmId(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param p = params.driverObject(Param.class);
            result.setData(alarmListService.findByAlarmId(p.getAlarmId()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/handle.do")
    public NTSTATUS handle(PDRIVER_OBJECT params, PRESULT_OBJECT result) {

        log.info("request params: {}", params);
        try {
            Param p = params.driverObject(Param.class);
            alarmListHandleService.handleAlarm(p.getId(), params.userID);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
